package com.ncmem.up7.biz.folder;


import com.ncmem.up7.model.FolderInf;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;


public class FolderDbWriter
{
    FolderInf root;//根目录
    Connection con;

    public FolderDbWriter(Connection con,FolderInf fd)
    {
        this.con = con;
        this.root = fd;
    }

    PreparedStatement makeCmd(Connection con) throws SQLException
    {
        StringBuilder sb = new StringBuilder();
        sb.append("insert into up7_folders(");
        sb.append(" f_id");
        sb.append(",f_nameLoc");
        sb.append(",f_pid");
        sb.append(",f_uid");
        sb.append(",f_lenLoc");
        sb.append(",f_sizeLoc");
        sb.append(",f_pathLoc");
        sb.append(",f_pathSvr");
        sb.append(",f_folders");
        sb.append(",f_fileCount");
        sb.append(",f_pidRoot");
        sb.append(") values(");
        sb.append(" ?");//f_id
        sb.append(",?");//f_nameLoc,
        sb.append(",?");//f_pid,
        sb.append(",?");//f_uid
        sb.append(",?");//f_lenLoc
        sb.append(",?");//f_sizeLoc
        sb.append(",?");//f_pathLoc
        sb.append(",?");//f_pathSvr
        sb.append(",?");//f_folders
        sb.append(",?");//f_fileCount
        sb.append(",?");//f_pidRoot
        sb.append(")");//

        PreparedStatement cmd = con.prepareStatement(sb.toString());
        cmd.setString(1, "");//f_id
        cmd.setString(2, "");//f_nameLoc
        cmd.setString(3, "");//f_pid
        cmd.setInt(4, 0);//f_uid
        cmd.setLong(5, 0);//f_lenLoc
        cmd.setString(6, "");//f_sizeLoc
        cmd.setString(7, "");//f_pathLoc
        cmd.setString(8, "");//f_pathSvr
        cmd.setInt(9, 0);//f_folders
        cmd.setInt(10, 0);//f_fileCount
        cmd.setString(11, "");//f_pidRoot
        return cmd;
    }

    public void save() throws SQLException
    {
        PreparedStatement cmd = this.makeCmd(con);

        //写根目录
        cmd.setString(1, this.root.id);//fd_id
        cmd.setString(2,this.root.nameLoc);
        cmd.setInt(3, 0);//fd_pid
        cmd.setInt(4, this.root.uid);//fd_uid
        cmd.setLong(5, this.root.lenLoc);//fd_length
        cmd.setString(6, this.root.sizeLoc);//fd_size
        cmd.setString(7, this.root.pathLoc);//fd_pathLoc
        cmd.setString(8, this.root.pathSvr);//fd_pathSvr
        cmd.setInt(9, this.root.foldersCount);//fd_folders
        cmd.setInt(10, this.root.filesCount);//fd_files
        cmd.setString(11, this.root.pidRoot);//fd_pidRoot
        cmd.execute();

        cmd.close();
    }
}
